package com.qfhd.soft.cjzcore.Core;

import android.content.Context;
import android.content.Intent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CJWatcher {

    public static final int listenTime = 2000;

    private Map<String,Long> watchMap = new HashMap<>();//假监听
    private Map<String,Long> watchMap_real = new HashMap<>();//真监听
    public List<String> list_packages = new ArrayList<>();
    private Context mContext;
    private boolean isStartSevice = false;//是否启动服务
    private String packageNameNow;
    private boolean isUseJiaTime = false;

    private static CJWatcher watcher;
    public static CJWatcher getInstance(){
        if(watcher == null){
            synchronized (CJWatcher.class){
                watcher = new CJWatcher();
            }
        }
        return watcher;
    }

    public void init(Context context){
        mContext = context;
    }

    public void setPackagename(String packagename){
        this.packageNameNow = packagename;
    }

    public void putWatch(String name){
        if(!list_packages.contains(name)){
            list_packages.add(name);
            CJLogger.v("watcher add sucess:" + name);
        }

        if(!isStartSevice){
            StartSevices();
        }
    }

    public void removeWatch(String name){
        if(list_packages.contains(name)){
            list_packages.remove(name);
        }

        if(watchMap.containsKey(name)){
            watchMap.remove(name);
        }

        if(watchMap_real.containsKey(name)){
            watchMap_real.remove(name);
        }

        if(list_packages.size() == 0){
//            mContext.stopService(new Intent(mContext,AppListener.class));
            stopServices();
        }
        CJLogger.v("removeWatch list_packages.size:" + list_packages.size());
    }

    public boolean isHavePackage(String packageName){
        if(list_packages.contains(packageName)){
            return true;
        }
        else{
            return false;
        }
    }

    public void setTime(String packageName){
        if(list_packages.contains(packageName)){
            if(watchMap_real.containsKey(packageName)){
                watchMap.put(packageName,CJTime.getCurrentTimeMillis()+CJTime.getDifferenceTime() - watchMap_real.get(packageName)*1000);
            }
            else{
                watchMap.put(packageName,CJTime.getCurrentTimeMillis()+CJTime.getDifferenceTime());
            }
        }
    }

    public boolean isHaveEnoughTime(long avoidTime,String packageName){
        if(list_packages.contains(packageName)){
            Long time = new Long(0);
            if(watchMap_real.containsKey(packageName) && !isUseJiaTime){
                //真实运行app时间
                time = watchMap_real.get(packageName);
                if(time >= avoidTime){
                    return true;
                }
                else{
                    return false;
                }
            }
            else{
                time = watchMap.get(packageName);
            }
            Long time_now = CJTime.getCurrentTimeMillis() + CJTime.getDifferenceTime();
            CJLogger.v("isHaveEnoughTime save time:" + CJTime.getFormatYYMMDDHHDDmm(time) + "time now:" + CJTime.getFormatYYMMDDHHDDmm(time_now));
            if((time_now - time) > avoidTime*1000){
                return true;
            }
            else{
                return false;
            }
        }
        else{
            return false;
        }
    }

    public long needTime(long avoidTime,String packageName){
        if(list_packages.contains(packageName)){
            Long time = 0L;
            if(watchMap_real.containsKey(packageName) && !isUseJiaTime){
                //真实app运行时间
                time = watchMap_real.get(packageName);
                if(time >= avoidTime){
                    return 0;
                }
                else{
                    return avoidTime - time ;
                }
            }
            else{
                time = watchMap.get(packageName);
            }
            Long time_now = CJTime.getCurrentTimeMillis() + CJTime.getDifferenceTime();
            CJLogger.v("save time:" + CJTime.getFormatYYMMDDHHDDmm(time) + "time now:" + CJTime.getFormatYYMMDDHHDDmm(time_now));
            time = time/1000;
            time_now = time_now/1000;

            if((time_now - time) >= avoidTime){
                return 0;
            }
            else{
                return avoidTime - (time_now - time) ;
            }
        }
        return -1;
    }

    public void beginListenApp(String packageName){
        //查询是否监听
        //添加时间
        if(list_packages.contains(packageName)){
            if(!watchMap_real.containsKey(packageName)){
                watchMap_real.put(packageName,listenTime/1000L);
                CJLogger.v("wather real add:" + packageName);
            }
            else{
                long time = watchMap_real.get(packageName);
                watchMap_real.put(packageName,time + listenTime/1000L);
                CJLogger.v("wather real time:" + (time + listenTime/1000L) + "秒");
            }

//            mContext.startService(new Intent(mContext,AppListener.class));
        }
        else{
            stopServices();
        }
    }

    public void stopServices(){
        if(isStartSevice){
            mContext.stopService(new Intent(mContext,AppListener.class));
            isStartSevice = false;
            isUseJiaTime = true;
            CJLogger.v("----stopServices----");
        }
    }

    public void StartSevices(){
        mContext.startService(new Intent(mContext,AppListener.class));
        isStartSevice = true;
        isUseJiaTime = false;
    }


    //当服务意外销毁时
    public void setServiceStateTop(){
        isStartSevice = false;
        isUseJiaTime = true;
        CJLogger.v("service stop");

        //赋值时间给假时间单位
        if(watchMap.containsKey(packageNameNow)){
            if(watchMap_real.containsKey(packageNameNow)){
                watchMap.put(packageNameNow,watchMap.get(packageNameNow) - watchMap_real.get(packageNameNow)*1000);
            }
        }
    }

    //当服务停止时，将假时间赋值的真实时间 重新计时
    public void retTime(long avoildTime){
        long needTime = needTime(avoildTime, packageNameNow);
        if (needTime == -1) {

        } else {
            if (watchMap_real.containsKey(packageNameNow)) {
                watchMap_real.put(packageNameNow, avoildTime - needTime);
            }
            if(isStartSevice){
                stopServices();
            }
        }
    }
}
